﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;

namespace Alfred
{
	class Metro
	{
		public List<Station> ListStations { get; set; }
		public List<int> ListLignes { get; set; }

		public static List<Station> GetStations(string from, string to, string type)
		{
			string request = "http://ratp-bridge2.fabernovel.com/ratp.itinerary.search?type1=adresse&type2=adresse&name1="+from+"&name2="+to+"&reseau=metro&traveltype="+type+"&datestart=true&datehour="+DateTime.Now.Hour+"&dateminute="+DateTime.Now.Minute;
			HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(request);
			myRequest.Timeout = 10000; // 10 secs
			myRequest.UserAgent = "Code Sample Web Client";
			myRequest.Accept = "text/xml";
			HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
			string ResponseText;
			using (StreamReader reader = new StreamReader(response.GetResponseStream()))
			{
				ResponseText = reader.ReadToEnd();
				XmlDocument doc = new XmlDocument();
				doc.LoadXml(ResponseText);
				List<Station> list = new List<Station>();

				int i = 0;

				XmlNodeList l = doc.GetElementsByTagName("stage");
				foreach (XmlElement stage in l)
				{
					i++;
					Station s = new Station();

					XmlNodeList param = stage.GetElementsByTagName("line");
					if (param.Count != 0)
						s.ligne = Int32.Parse(param[0].InnerText) - 6;

					s.debut = stage.GetElementsByTagName("name")[0].InnerText;
					
					if (stage.GetElementsByTagName("direction").Count != 0)
						s.direction = stage.GetElementsByTagName("direction")[0].InnerText;

					if (stage.GetElementsByTagName("to").Count != 0)
					{
						XmlElement el = (XmlElement)stage.GetElementsByTagName("to")[0];
						s.arret = el.GetElementsByTagName("name")[0].InnerText;
					}

					s.type = Station.typeStation.CORRESPONDANCE;
					
					if (stage.HasAttribute("start"))
						s.type = Station.typeStation.DEPART;
					
					if (i == l.Count - 1)
						s.type = Station.typeStation.ARRIVEE;
					
					if (i == l.Count)
						s.type = Station.typeStation.AUTRE;

					s.rank = i;

					list.Add(s);
				}
				return list;
			}			
		}

		public static string GetStringItinerary(string from, string to, string type)
		{
			string result = "Prendre la ligne ";

			List<Station> stations = GetStations(from, to, type);
			foreach (Station s in stations)
			{
                if (s.rank == 1 || s.rank == stations.Count)
                    continue;
				if (s.type == Station.typeStation.AUTRE)
					continue;
                if (s.rank == 2)
                    result += s.ligne + ", à " + s.debut + ", vers " + s.direction;
				if (s.type == Station.typeStation.CORRESPONDANCE && s.rank != 2 && s.direction != null)
					result += ", changer à " + s.debut + ", puis prendre la " + s.ligne + ", vers " + s.direction;
                if (s.type == Station.typeStation.ARRIVEE && s.rank != 2)
                    result += ", changer à " + s.debut + ", puis prendre la " + s.ligne + ", vers " + s.direction + ", puis sortir à " + s.arret;
			}
			return result;
		}
	}

	

	class Station
	{
		public string debut {get;set;}
		public typeStation type { get; set; }
		public int ligne { get; set; }
		public string direction { get; set; }
		public string arret { get; set; }
		public int rank { get; set; }

		public enum typeStation { DEPART, CORRESPONDANCE, ARRIVEE, AUTRE };
	}
}
